package com.dashu.core.controller;

import com.dashu.core.result.PageEnty;
import com.dashu.core.result.ResultBean;
import com.dashu.core.dto.UserDTO;
import com.dashu.core.dto.UserLoginDTO;
import com.dashu.core.service.UserService;
import com.dashu.base.enums.ReplRyCode;
import com.dashu.core.vo.UpdateEntityVO;
import com.dashu.core.vo.user.LoginUserVo;
import com.dashu.core.vo.user.UserPassVo;
import com.dashu.core.vo.user.UserSubordinateVo;
import com.dashu.core.vo.user.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Author xys
 * @Date 2020/11/17 15:21
 */

@RequestMapping("dashu/user")
@RestController
@Slf4j
@Api(value = "dashu/user", tags = {"账号管理模块"}, description = "后台")
public class UserController {

    @Autowired
    private UserService userService;



    @PostMapping("/login")
    @ApiOperation(value = "账号登录", notes = "首页账号登录")
    public ResultBean<UserLoginDTO> login(@RequestBody @Validated LoginUserVo loginUserVo) {
        log.info(String.valueOf(loginUserVo));
        if (loginUserVo == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        UserLoginDTO userLoginDTO = userService.login(loginUserVo.getUserName(), loginUserVo.getPassword());
        if (userLoginDTO != null) {
            return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "首页账号登录", userLoginDTO);
        }
        return new ResultBean<>(ReplRyCode.DATA_IDORPAS.getCode(), ReplRyCode.DATA_IDORPAS.getMessage(), null);


    }

    @PostMapping("/addUser")
    @ApiOperation(value = "账号添加", notes = "账号添加")
    public ResultBean<UserDTO> addUser(@RequestBody @Validated UserVo userVo) throws Exception {
        UserDTO userDTO = userService.existByUserName(userVo.getUserName());
        if (userDTO != null) {
            return new ResultBean<>(ReplRyCode.EXIST_USER.getCode(), ReplRyCode.EXIST_USER.getMessage(), null);
        }
        if (userVo == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "账号添加", userService.add(userVo));
    }

    @PostMapping("/updataPass")
    @ApiOperation(value = "账号修改密码", notes = "账号修改密码")
    public ResultBean<Boolean> updataPass(@RequestBody @Validated UpdateEntityVO<UserPassVo> updataUserVo) throws Exception {
        if (updataUserVo == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "账号修改密码", userService.updataUser(updataUserVo));
    }

    @PostMapping("/updataSubordinate")
    @ApiOperation(value = "账号修改管控", notes = "账号修改管控")
    public ResultBean<Boolean> updataSubordinate(@RequestBody @Validated UpdateEntityVO<UserSubordinateVo> userSubordinateVo) throws Exception {
        if (userSubordinateVo == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "账号修改管控", userService.updataSubordinate(userSubordinateVo));
    }

    @PostMapping("/findCreteUser")
    @ApiOperation(value = "子账号查询", notes = "子账号查询")
    public ResultBean<List<UserDTO>> findCreteUser(@RequestBody @Validated PageEnty<Long> creteUserVo) throws Exception {
        if (creteUserVo == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "子账号查询", userService.findByCreteUser(creteUserVo));

    }

    @GetMapping("/findUser")
    @ApiOperation(value = "账号查询", notes = "账号查询")
    public ResultBean<UserDTO> findUser(Long userId) {
        if (userId == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }

        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "账号查询", userService.findByUserId(userId));
    }


    @DeleteMapping("/deleteUser")
    @ApiOperation(value = "账号删除", notes = "账号删除")
    public ResultBean<Boolean> deleteUser(Long userId) {
        if (userId == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "账号删除", userService.deleteByUserId(userId));

    }

}
